//
//  UserInfoCell.m
//  HotWord
//
//  Created by Rainbow on 7/26/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "UserInfoCell.h"

@implementation UserInfoCell
@synthesize userInfoTitle;
@synthesize userInfoTextField;
@synthesize userInfoLabel;
@synthesize delegate;
@synthesize indexPath = _indexPath;
@synthesize shadowView;
@synthesize statusTextView;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)awakeFromNib
{
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.userInfoTextField.delegate = self;  
    self.statusTextView.delegate = self;
    UITapGestureRecognizer *tapGesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnValueLabel)] autorelease];
    [self.userInfoLabel addGestureRecognizer:tapGesture];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    if (self.delegate && [self.delegate respondsToSelector:@selector(UserInfoCellBeginEdit:)]) {
        [self.delegate UserInfoCellBeginEdit:self];
    }
    if (!_value || [@"" isEqualToString:_value])
    {
        switch (self.indexPath.row) {
            case 1:
                self.userInfoLabel.text = nil;
                self.userInfoLabel.textColor = [UIColor whiteColor];
                
                break;
            case 2:
                self.userInfoLabel.text = nil;
                self.userInfoLabel.textColor = [UIColor whiteColor];
                
                break;
            case 3:
                self.userInfoTextField.text = nil;
                self.userInfoTextField.textColor = [UIColor whiteColor];
                
                break;
            case 4:
                self.userInfoTextField.text = nil;
                self.userInfoTextField.textColor = [UIColor whiteColor];
                
                break;
            case 5:
                self.userInfoTextField.text = nil;
                self.userInfoTextField.textColor = [UIColor whiteColor];
                
                break;
            case 6:
                self.userInfoLabel.text = nil;
                self.userInfoLabel.textColor = [UIColor whiteColor];
                
                break;
            case 7:
                self.statusTextView.text = nil;
                self.statusTextView.textColor = [UIColor whiteColor];
                
                break;
                
            default:
                break;
        }
    }

}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(UserInfoCellEndEdit:)]) {
        [self.delegate UserInfoCellEndEdit:self];
    }
}

-(BOOL)textFieldShouldReturn:(UITextField*)textField;
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(UserInfoCellEndEdit:)]) {
        [self.delegate UserInfoCellEndEdit:self];
    }
    [self.userInfoTextField resignFirstResponder];
    return NO;
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(UserInfoCellBeginEdit:)]) {
        [self.delegate UserInfoCellBeginEdit:self];
    }
    if (!_value || [@"" isEqualToString:_value])
    {
        switch (self.indexPath.row) {
            case 1:
                self.userInfoLabel.text = nil;
                self.userInfoLabel.textColor = [UIColor whiteColor];
                
                break;
            case 2:
                self.userInfoLabel.text = nil;
                self.userInfoLabel.textColor = [UIColor whiteColor];
                
                break;
            case 3:
                self.userInfoTextField.text = nil;
                self.userInfoTextField.textColor = [UIColor whiteColor];
                
                break;
            case 4:
                self.userInfoTextField.text = nil;
                self.userInfoTextField.textColor = [UIColor whiteColor];
                
                break;
            case 5:
                self.userInfoTextField.text = nil;
                self.userInfoTextField.textColor = [UIColor whiteColor];
                
                break;
            case 6:
                self.userInfoLabel.text = nil;
                self.userInfoLabel.textColor = [UIColor whiteColor];
                
                break;
            case 7:
                self.statusTextView.text = nil;
                self.statusTextView.textColor = [UIColor whiteColor];
                
                break;
                
            default:
                break;
        }
    }

}
- (void)textViewDidEndEditing:(UITextView *)textView
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(UserInfoCellEndEdit:)]) {
        [self.delegate UserInfoCellEndEdit:self];
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if (range.location >= 140)
    {
        return  NO;
    }
    else
    {
        return YES;
    }
}

- (void)setInfoTitle:(NSString *)title infoValue:(NSString *)value pickMode:(BOOL)pickMode
{
    if (_value) {
        [_value release];
        _value = nil;
    }
    _value = [value retain];
    _pickMode = pickMode;
    self.userInfoTitle.text = title;
}

- (void)setIndexPath:(NSIndexPath *)indexPath
{
    if (_indexPath) {
        [_indexPath release];
        _indexPath = nil;
    }
    _indexPath = [indexPath retain];
    if(_indexPath.row == 7)
    {
        self.statusTextView.hidden = NO;
        self.userInfoTextField.hidden = YES;
        self.statusTextView.text = _value;
    }
    else {
        
        if (_pickMode) {
            self.userInfoLabel.hidden = NO;
            self.userInfoTextField.hidden = YES;
            self.userInfoLabel.text = _value;
        }
        else
        {
            self.userInfoLabel.hidden = YES;
            self.userInfoTextField.hidden = NO;
            self.userInfoTextField.text = _value;
        }
        self.statusTextView.hidden = YES;
    }
}

- (void)setIsEditing:(BOOL)isEditing
{
    if (isEditing) {
        self.userInfoTextField.enabled = YES;
        self.statusTextView.editable = YES;
        self.shadowView.hidden = NO;
        self.userInfoLabel.userInteractionEnabled = YES;
    }
    else {
        self.userInfoTextField.enabled = NO;
        self.statusTextView.editable = NO;
        self.shadowView.hidden = YES;
        self.userInfoLabel.userInteractionEnabled = NO;
    }
    if (!_value || [@"" isEqualToString:_value])
    {
        if (isEditing) {
            switch (self.indexPath.row) {
                case 1:
                    self.userInfoLabel.text = @"Tap and Choose,then Press \"ok!\"";
                    self.userInfoLabel.textColor = [UIColor redColor];
                    break;
                case 2:
                    self.userInfoLabel.text = @"Tap and Choose,then Press \"ok!\"";
                    self.userInfoLabel.textColor = [UIColor redColor];
                    
                    break;
                case 3:
                    self.userInfoLabel.text = @"Tap and Choose,then Press \"ok!\"";
                    self.userInfoLabel.textColor = [UIColor redColor];
                    
                    break;
                case 4:
                    self.userInfoTextField.text = @"Tap and Enter,then Press \"ok!\"";
                    self.userInfoTextField.textColor = [UIColor redColor];
                    
                    break;
                case 5:
                    self.userInfoTextField.text = @"Tap and Enter,then Press \"ok!\"";
                    self.userInfoTextField.textColor = [UIColor redColor];
                    
                    break;
                case 6:
                    self.userInfoLabel.text = @"Tap and Choose,then Press \"ok!\"";
                    self.userInfoLabel.textColor = [UIColor redColor];
                    
                    break;
                case 7:
                    self.statusTextView.text = @"Tap and Enter,then Press \"ok!\"";
                    self.statusTextView.textColor = [UIColor redColor];
                    
                    break;
                    
                default:
                    break;
            }
        }
        else {
            switch (self.indexPath.row) {
                case 1:
                    self.userInfoLabel.text = nil;
                    self.userInfoLabel.textColor = [UIColor whiteColor];
                    
                    break;
                case 2:
                    self.userInfoLabel.text = nil;
                    self.userInfoLabel.textColor = [UIColor whiteColor];
                    
                    break;
                case 3:
                    self.userInfoLabel.text = nil;
                    self.userInfoLabel.textColor = [UIColor whiteColor];
                    
                    break;
                case 4:
                    self.userInfoTextField.text = nil;
                    self.userInfoTextField.textColor = [UIColor whiteColor];
                    
                    break;
                case 5:
                    self.userInfoTextField.text = nil;
                    self.userInfoTextField.textColor = [UIColor whiteColor];
                    
                    break;
                case 6:
                    self.userInfoLabel.text = nil;
                    self.userInfoLabel.textColor = [UIColor whiteColor];
                    
                    break;
                case 7:
                    self.statusTextView.text = nil;
                    self.statusTextView.textColor = [UIColor whiteColor];
                    
                    break;
                    
                default:
                    break;
            }
        }

    }
}

- (void)tapOnValueLabel
{
    if (self.indexPath.row == 6) {
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(UserInfoCellChooseDreamSchool:)]) {
            [self.delegate UserInfoCellChooseDreamSchool:self];
        }
    }
    else {
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(UserInfoCellShowPicker:)]) {
            [self.delegate UserInfoCellShowPicker:self];
        }
    }
}

- (void)dealloc {
    [_value release];
    [_indexPath release];
    [userInfoTitle release];
    [userInfoTextField release];
    [userInfoLabel release];
    [shadowView release];
    [statusTextView release];
    [super dealloc];
}
@end
